import { lazy } from 'react';
import BrandInfoApp from "@pages/BrandInfoApp";


/**
 * 根据权限删除路由
 * @param bizType
 * @param isAdmin
 * @param isBrand
 */
export const deleteRouteByPermission = ({ bizType, isAdmin, isBrand }, route = []) => {
    let _route = []
    route.forEach(item => {
        if (item.valid && !item.valid({ bizType, isAdmin, isBrand })) return;
        if (item.childRoutes) {
            let _childRoutes = deleteRouteByPermission({ bizType, isAdmin, isBrand }, item.childRoutes)
            if (!_childRoutes.length) return ;
            _route.push({
                ...item,
                childRoutes: _childRoutes
            })
        } else {
            _route.push({
                ...item
            })
        }
    })
    return _route
}

/**
 * 根据权限重组路由
 * @param bizType
 * @param isAdmin
 * @param isBrand
 */
export const reformedRoute = ({ bizType, isAdmin, isBrand }) => {
    brandStoreConfig = deleteRouteByPermission({ bizType, isAdmin, isBrand }, allBrandStoreConfig)
    console.log('brandStoreConfig', brandStoreConfig)
}

let config = [
    {
        path: '/login',
        exact: true,
        component: lazy(() => import('@/pages/login/LoginPage')),
    },
    {
        path: '/mobile-login',
        exact: true,
        component: lazy(() => import('@/pages/mobile/MobileLoginPage')),
    },
    {
        path: '/employee/invite',
        exact: true,
        component: lazy(() => import('@/pages/mobile/EmployeeInvite')),
    },
    {
        path: '/employee/redirect',
        exact: true,
        component: lazy(() => import('@/pages/mobile/EmployeeRedirect')),
    },
    {
        path: '/employee/join',
        exact: true,
        component: lazy(() => import('@/pages/mobile/EmployeeJoin')),
    },
    {
        path: '/brand-list',
        exact: true,
        component: lazy(() => import('@/pages/login/BrandMgtPage')),
    },
    {
        path: '/brand-edit/:brandId',
        exact: true,
        component: lazy(() => import('@/pages/login/BrandEditPage')),
    },
    {
        path: '/brand-create',
        exact: true,
        component: lazy(() => import('@/pages/login/BrandEditPage')),
    },
    {
        path: '/user-setting',
        exact: true,
        component: lazy(() => import('@/pages/login/AccountPage')),
    },
    {
        path: '/logout',
        exact: true,
        component: lazy(() => import('@pages/login/LogoutPage'))
    },
    { path: '/', component: BrandInfoApp },
]

let allBrandStoreConfig = [
    {
        path: '/store-list',
        valid: ({isAdmin, isBrand}) => isAdmin && isBrand,
        childRoutes: [
            {
                path: '/store-list',
                exact: true,
                component: lazy(() => import('@/pages/store/StoreListPage')),
            },
            {
                path: '/store-list/create',
                exact: true,
                component: lazy(() => import('@/pages/store/StoreDetailPage')),
            },
            {
                path: '/store-list/:storeId/edit',
                exact: true,
                component: lazy(() => import('@/pages/store/StoreDetailPage')),
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/shopping-mall',
        childRoutes: [
            {
                path: '/shopping-mall/open',
                valid: ({bizType, isAdmin}) => bizType === null && isAdmin,
                exact: true,
                component: lazy(() => import('@pages/shoppingMall/OpenShoppingMall')),
            },
            {
                path: '/shopping-mall/order/:orderNumber/detail',
                exact: true,
                valid: ({bizType}) => bizType !== null,
                component: lazy(() => import('@pages/shoppingMall/ShoppingMallOrderDetailPage')),
            },
            {
                path: '/shopping-mall/after-sales/:applyNumber/detail',
                exact: true,
                valid: ({bizType}) => bizType !== null,
                component: lazy(() => import('@pages/shoppingMall/ShoppingMallAfterSalesOrderDetailPage')),
            },
            {
                path: '/shopping-mall/design/:platformId/detail',
                component: lazy(() => import('@pages/shoppingMall/ShoppingMallDesignDetailPage')),
                valid: ({bizType}) => bizType !== null,
                childRoutes: [{
                    path: '/shopping-mall/design/:platformId/detail/global',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/GlobalDesign')),
                }, {
                    path: '/shopping-mall/design/:platformId/detail/start-page',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/StartPageDesign')),
                }, {
                    path: '/shopping-mall/design/:platformId/detail/tabbar',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/TabbarDesign')),
                }, {
                    path: '/shopping-mall/design/:platformId/detail/special',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/SpecialDesign')),
                }, {
                    path: '/shopping-mall/design/:platformId/detail/cart',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/CartDesign')),
                }, {
                    path: '/shopping-mall/design/:platformId/detail/owner',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/OwnerDesign')),
                }, {
                    path: '/shopping-mall/design/:platformId/detail/home',
                    exact: true,
                    component: lazy(() => import('@pages/shoppingMall/design/HomeDesign')),
                }]
            },
            {
                path: '/shopping-mall/product/:productId/edit',
                valid: ({bizType}) => bizType !== null,
                component: lazy(() => import('@/pages/shoppingMall/ShoppingMallProductDetailPage')),
            },
            {
                path: '',
                valid: ({bizType}) => bizType !== null,
                component: lazy(() => import('@pages/App')),
                childRoutes: [
                    {
                        path: '/shopping-mall/product',
                        exact: true,
                        component: lazy(() => import('@/pages/shoppingMall/ShoppingMallProductPage')),
                    }, {
                        path: '/shopping-mall/order',
                        exact: true,
                        component: lazy(() => import('@/pages/shoppingMall/ShoppingMallOrderPage')),
                    }, {
                        path: '/shopping-mall/after-sales',
                        exact: true,
                        component: lazy(() => import('@/pages/shoppingMall/ShoppingMallAfterSalesOrderPage')),
                    }, {
                        path: '/shopping-mall/design',
                        exact: true,
                        component: lazy(() => import('@/pages/shoppingMall/ShoppingMallDesignPage')),
                    }, {
                        path: '/shopping-mall/settings',
                        exact: true,
                        component: lazy(() => import('@/pages/shoppingMall/ShoppingMallSettingsPage')),
                    }
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/product-center',
        childRoutes: [
            {
                path: '/product-center/type',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/ProductTypeChoosePage')),
            },
            {
                path: '/product-center/supply-group',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/SupplyGroupPage')),
            },
            {
                path: '/product-center/supply-group/:id/detail',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/SupplyGroupProductPage')),
            },
            {
                path: '/product-center/product/:productNature/detail/:productId/edit',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/ProductDetailPage')),
            },
            {
                path: '/product-center/product/:productNature/detail/create',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/ProductDetailPage')),
            },
            {
                path: '/product-center/product/:productNature/:productId/detail',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/ProductDetailPage')),
            },
            {
                path: '/product-center/product/:productNature/import',
                exact: true,
                component: lazy(() => import('@/pages/productCenter/ProductImportPage')),
            },
            {
                path: '/product-center/product/:productNature',
                component: lazy(() => import('@/pages/productCenter/ProductMgtPage')),
                childRoutes: [
                    {
                        path: '/product-center/product/:productNature/list',
                        exact: true,
                        component: lazy(() => import('@/pages/productCenter/ProductListPage')),
                    }, {
                        path: '/product-center/product/:productNature/type',
                        exact: true,
                        component: lazy(() => import('@/pages/productCenter/ProductTypeListPage')),
                    }, {
                        path: '/product-center/product/:productNature/property',
                        exact: true,
                        component: lazy(() => import('@/pages/productCenter/ProductPropertyListPage')),
                    }
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/employee',
        valid: ({isAdmin}) => isAdmin,
        childRoutes: [
            {
                path: '/employee/create',
                exact: true,
                component: lazy(() => import('@/pages/employee/EmployeeCreatePage')),
            },
            {
                path: '/employee/edit/:id',
                exact: true,
                component: lazy(() => import('@pages/employee/EmployeeCreatePage')),
            },
            {
                path: '/employee/role/create',
                exact: true,
                component: lazy(() => import('@/pages/employee/EmployeeRoleCreatePage')),
            },
            {
                path: '/employee/role/edit/:id',
                exact: true,
                component: lazy(() => import('@/pages/employee/EmployeeRoleCreatePage')),
            },
            {
                path: '/employee',
                component: lazy(() => import('@/pages/employee/EmployeePage')),
                childRoutes: [
                    {
                        path: '/employee/list',
                        exact: true,
                        component: lazy(() => import('@/pages/employee/EmployeeListPage')),
                    }, {
                        path: '/employee/role',
                        exact: true,
                        component: lazy(() => import('@/pages/employee/EmployeeRolePage')),
                    },
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/payment',
        valid: ({isAdmin}) => isAdmin,
        childRoutes: [
            {
                path: '/payment/wechat/create/:type',
                exact: true,
                component: lazy(() => import('@/pages/payment/PaymentWechatCreate')),
            },
            {
                path: '/payment/wechat/modify/:type/:id',
                exact: true,
                component: lazy(() => import('@/pages/payment/PaymentWechatCreate')),
            },
            {
                path: '/payment/alipay/create/:id',
                exact: true,
                component: lazy(() => import('@/pages/payment/PaymentAlipayCreate')),
            },
            {
                path: '/payment/alipay/modify/:id',
                exact: true,
                component: lazy(() => import('@/pages/payment/PaymentAlipayCreate')),
            },
            {
                path: '/payment',
                component: lazy(() => import('@/pages/payment/PaymentPage')),
                childRoutes: [
                    {
                        path: '/payment/wechat',
                        exact: true,
                        component: lazy(() => import('@/pages/payment/PaymentWechatPage')),
                    }, {
                        path: '/payment/alipay',
                        exact: true,
                        component: lazy(() => import('@/pages/payment/PaymentAlipayPage')),
                    }, {
                        path: '/payment/config',
                        exact: true,
                        component: lazy(() => import('@/pages/payment/PaymentConfigPage')),
                    },
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/warehouse',
        childRoutes: [
            {
                path: '/warehouse/order/:warehouseId/:orderNumber/detail',
                exact: true,
                component: lazy(() => import('@pages/warehouse/WarehouseOrderDetailPage')),
            },
            {
                path: '/warehouse/refund/:warehouseId/:orderNumber/detail',
                exact: true,
                component: lazy(() => import('@pages/warehouse/WarehouseRefundDetailPage')),
            },
            {
                path: '/warehouse/create',
                component: lazy(() => import('@pages/warehouse/WarehouseCreate')),
            },
            {
                path: '/warehouse/edit/:id',
                component: lazy(() => import('@pages/warehouse/WarehouseCreate')),
            },
            {
                path: '/warehouse/check',
                component: lazy(() => import('@pages/warehouse/CheckInventory')),
            },
            {
                path: '/warehouse/check-edit/:id',
                component: lazy(() => import('@pages/warehouse/CheckInventory')),
            },
            {
                path: '/warehouse/check-detail/:id',
                component: lazy(() => import('@pages/warehouse/CheckInventoryDetail')),
            },
            {
                path: '/warehouse/purchase-create',
                component: lazy(() => import('@pages/warehouse/PurchaseCreate')),
            },
            {
                path: '/warehouse/purchase-detail/:id',
                component: lazy(() => import('@pages/warehouse/PurchaseDetail')),
            },
            {
                path: '/warehouse/purchase-back/:id',
                component: lazy(() => import('@pages/warehouse/PurchaseBack')),
            },
            {
                path: '/warehouse/schedule-create',
                component: lazy(() => import('@pages/warehouse/ScheduleCreate')),
            },
            {
                path: '/warehouse/schedule-detail/:id',
                component: lazy(() => import('@pages/warehouse/ScheduleDetail')),
            },
            {
                path: '/warehouse/schedule-agree/:id',
                component: lazy(() => import('@pages/warehouse/ScheduleAgree')),
            },
            {
                path: '/warehouse/schedule-in/:id',
                component: lazy(() => import('@pages/warehouse/ScheduleIn')),
            },
            {
                path: '/warehouse/setting/:id',
                component: lazy(() => import('@pages/warehouse/WarehouseSetting')),
            },
            {
                path: '/warehouse',
                component: lazy(() => import('@pages/WarehouseApp')),
                childRoutes: [
                    {
                        path: '/warehouse/manage',
                        exact: true,
                        component: lazy(() => import('@pages/warehouse/WarehouseManage')),
                    },
                    {
                        path: '/warehouse/order',
                        component: lazy(() => import('@pages/warehouse/WarehouseOrderPage')),
                    },
                    {
                        path: '/warehouse/refund',
                        component: lazy(() => import('@pages/warehouse/WarehouseRefundPage')),
                    },
                    {
                        path: '/warehouse/stock',
                        exact: true,
                        component: lazy(() => import('@pages/warehouse/StockManage')),
                    },
                    {
                        path: '/warehouse/purchase',
                        exact: true,
                        component: lazy(() => import('@pages/warehouse/PurchaseManage')),
                    },
                    {
                        path: '/warehouse/schedule',
                        exact: true,
                        component: lazy(() => import('@pages/warehouse/ScheduleManage')),
                    },
                    {
                        path: '/warehouse/sales',
                        exact: true,
                        component: lazy(() => import('@pages/warehouse/SalesList')),
                    },
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/marketing',
        childRoutes: [
            {
                path: '/marketing/coupon/:type/create',
                exact: true,
                component: lazy(() => import('@pages/marketing/MarketingCouponCreate')),
            },
            {
                path: '/marketing/coupon/:type/modify/:id',
                exact: true,
                component: lazy(() => import('@pages/marketing/MarketingCouponCreate')),
            },
            {
                path: '/marketing/promotion/:type/create',
                exact: true,
                component: lazy(() => import('@pages/marketing/MarketingPromotionCreate')),
            },
            {
                path: '/marketing/promotion/:type/modify/:id',
                exact: true,
                component: lazy(() => import('@pages/marketing/MarketingPromotionCreate')),
            },
            {
                path: '/marketing/coupon/distribution/:id',
                exact: true,
                component: lazy(() => import('@pages/marketing/MarketingCouponDistribution')),
            },
            {
                path: '',
                component: lazy(() => import('@pages/App')),
                childRoutes: [
                    {
                        path: '/marketing',
                        exact: true,
                        component: lazy(() => import('@pages/marketing/MarketingPage')),
                    },
                    {
                        path: '/marketing/coupon',
                        exact: true,
                        component: lazy(() => import('@pages/marketing/MarketingCouponPage')),
                    },
                    {
                        path: '/marketing/promotion/:type',
                        exact: true,
                        component: lazy(() => import('@pages/marketing/MarketingPromotionPage')),
                    },
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/customer',
        childRoutes: [
            {
                path: '/customer/member/create',
                exact: true,
                component: lazy(() => import('@pages/customer/CustomerMemberCreatePage')),
            },
            {
                path: '/customer/member/edit/:id',
                exact: true,
                component: lazy(() => import('@pages/customer/CustomerMemberCreatePage')),
            },
            {
                path: '',
                component: lazy(() => import('@pages/App')),
                childRoutes: [
                    {
                        path: '/customer',
                        exact: true,
                        component: lazy(() => import('@pages/customer/CustomerMemberPage')),
                    }, {
                        path: '/customer/list',
                        exact: true,
                        component: lazy(() => import('@pages/customer/CustomerListPage')),
                    }, {
                        path: '/customer/detail/:id',
                        exact: true,
                        component: lazy(() => import('@pages/customer/CustomerDetailPage')),
                    }
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/third-party',
        valid: ({isBrand}) => isBrand,
        childRoutes: [
            {
                path: '/third-party/alipay/replenish',
                exact: true,
                component: lazy(() => import('@pages/thirdparty/alipay/AlipayReplenishInfo')),
            },
            {
                path: '',
                component: lazy(() => import('@pages/App')),
                childRoutes: [
                    {
                        path: '/third-party/wechat',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/wechat/IntroductionPage'))
                    }, {
                        path: '/third-party/wechat/release',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/wechat/WechatReleasePage'))
                    }, {
                        path: '/third-party/wecom',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/wecom/IntroductionPage'))
                    }, {
                        path: '/third-party/wecom/release',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/wecom/WecomReleasePage'))
                    }, {
                        path: '/third-party/alipay',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/alipay/IntroductionPage'))
                    }, {
                        path: '/third-party/alipay/release',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/alipay/AlipayReleasePage'))
                    }, {
                        path: '/third-party/bytedance',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/byteDance/IntroductionPage'))
                    }, {
                        path: '/third-party/bytedance/release',
                        exact: true,
                        component: lazy(() => import('@pages/thirdparty/byteDance/BytedanceReleasePage'))
                    }
                ]
            },
            { path: '*', redirect: '/main' },
        ]
    },
    {
        path: '/',
        component: lazy(() => import('@pages/App')),
        childRoutes: [
            {
                path: '/main',
                exact: true,
                component: lazy(() => import('@pages/home/Home')),
            },
            {
                path: '/statistics',
                exact: true,
                component: lazy(() => import('@pages/statistics/StatisticsPage')),
            },
            {
                path: '/live',
                exact: true,
                component: lazy(() => import('@pages/live/IntroductionPage')),
            },
            {
                path: '/star-group',
                exact: true,
                component: lazy(() => import('@pages/starGroup/IntroductionPage')),
            },
            { path: '*', redirect: '/main' },
        ]
    },
    { path: '*', redirect: '/main' },
]
let brandStoreConfig = allBrandStoreConfig
export const getBrandInfoConfig = () => brandStoreConfig

export default config;